﻿Imports System.Security.Cryptography
Imports System.Text.Encoding
Imports HotelLibrary


Public Class CustomerFunction
    Dim db As New DBConnect
    Dim ds As New DataSet
    Function hashFunction(ByVal passwordStr As String) As String

        Dim h As HashAlgorithm = New SHA256Managed

        Dim passwordByte(), hashedPasswordByte() As Byte

        passwordByte = Unicode.GetBytes(passwordStr)

        hashedPasswordByte = h.ComputeHash(passwordByte)

        Return hashByteToString(hashedPasswordByte)
    End Function

    Function hashByteToString(ByRef hashedByteArray() As Byte) As String
        Dim hexStr As String = ""

        Dim x As Integer
        For x = LBound(hashedByteArray) To UBound(hashedByteArray)
            hexStr &= Hex$(hashedByteArray(x))
        Next x

        Return hexStr

    End Function

    Function insertCustomer(cust As CustLib) As Boolean
        Dim strSql As String = "Insert into Customer_t (LoginID, FirstName, LastName, Address, City, State, ZipCode, Email, Password) values ('" & cust.LoginID & "','" & cust.FirstName & "','" & cust.LastName & "','" & cust.Address & "','" & cust.City & "','" & cust.State & "','" & cust.ZipCode & "','" & cust.Email & "','" & cust.Password & "')"

        If (db.doUpdate(strSql) > 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Function getCustomer(userID As String) As DataSet
        ds = New DataSet
        Dim strSql As String = "Select * from Customer_t where LoginID = '" & userID & "'"
        ds = db.getDataSet(strSql)
        Return ds
    End Function

    Function chckCustomer(p1 As String, hash As String) As Integer
        ds = New DataSet
        ds = getCustomer(p1)
        If ((ds.Tables(0).Rows.Count) < 0) Then
            Return 0
        Else
            If ((ds.Tables(0).Rows(0).Item(8).ToString) = hash) Then
                Return 1
            Else
                Return -1
            End If
        End If
    End Function


End Class
